/**     
 * @Title: MessageTrunk.java   
 * @Package me.xxy.mq   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年6月1日 下午2:31:02   
 * @version V1.0     
 */
package me.xxy.mq;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import redis.clients.jedis.JedisPubSub;

import com.alibaba.fastjson.JSON;

/**
 * @ClassName: MessageTrunk
 * @Description: TODO
 * @author weiwei
 * @date 2018年6月1日 下午2:31:02
 * 
 */
@Service
public class MessageTrunk {

	private static final Log LOG = LogFactory.getLog(MessageTrunk.class);

	@Autowired
	private RedisConfig redisConfig;

	/**
	 * @Title: subscribed
	 * @Description: 消费
	 * @param jedisPubSub
	 * @param channels
	 * @Reutrn void
	 */
	public void subscribed(JedisPubSub jedisPubSub, String... channels) {
		LOG.info("JedisPubSub >> " + jedisPubSub);
		LOG.info("channels >> " + channels);
		redisConfig.subscribed(jedisPubSub, channels);
	}

	/**
	 * @Title: publish
	 * @Description: 发布
	 * @param message
	 * @Reutrn Long
	 */
	public Long publish(Message message) {
		LOG.info("Message >> " + message);
		/** 频道 **/
		String channel = message.getChannel();
		if (StringUtils.isEmpty(channel)) {
			LOG.warn("Message's channel is empty!");
			return 0L;
		}
		/** 消息具体内容 **/
		Object content = message.getContent();
		if (StringUtils.isEmpty(content)) {
			LOG.warn("Message's content is empty!");
			return 0L;
		}
		return redisConfig.publish(message.getChannel(), JSON.toJSONString(message.getContent()));
	}
}
